package fy.mdm.dao;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;
import fy.config.FyConfig;
import fy.mdm.bean.PartEditCustomerBean;
import fy.mdm.bean.PartEditCustomerFBBean;
import fy.util.DBUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PartEditCustomerFBDao {
    private PartEditCustomerFBBean partEditCustomerFBBean;
    private static Log log = LogFactory.getLog(FiexdAssetReviewDao.class);
    public boolean StateReturn() throws Exception{
        try {

            IServiceRequester Requester = BaseServiceRequester.getInstance();
            List<GroupRecord> grs1 = new ArrayList<GroupRecord>();//多层用
            List<GroupRecord> grs2 = new ArrayList<GroupRecord>();
            // 定义请求报文对象
            IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
            String SourceSysID = "06004"; // 请求方(发布方)
            String ServiceID = "06001000000007";// 响应方(发布方)
            // 获取系统时间
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String format = simpleDateFormat.format(date);
            // 对请求报文对象进行设置
            reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
            reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
                    + String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
            reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
            reqMo.setServiceDateTime(format); // 设置报文 日期时间
            reqMo.setReqValue("BOID","SOA_MDM_CUSTOMER_F" );
            reqMo.setReqValue("ControlType", "6");
            GroupRecord gr1 = new GroupRecord();
            gr1.setName("ROW");
            gr1.setFieldValue("MDM_CODE",this.partEditCustomerFBBean.getCUSTOME_CODE());
            gr1.setFieldValue("CUSTOME_NAME",this.partEditCustomerFBBean.getCUSTOME_NAME());
            gr1.setFieldValue("COUNTRY",getEnumValue("客户国别",this.partEditCustomerFBBean.getCOUNTRY()));
            gr1.setFieldValue("CUSTOMER_ATTRIBUTE",getEnumValue("客户属性",this.partEditCustomerFBBean.getCUSTOMER_ATTRIBUTE()));
            gr1.setFieldValue("CUSTOMER_TYPE",getEnumValue("客户系别",this.partEditCustomerFBBean.getCUSTOMER_TYPE()));
            gr1.setFieldValue("BIG_TYPE",getEnumValue("客户大类",this.partEditCustomerFBBean.getBIG_TYPE()));
            gr1.setFieldValue("SMALL_TYPE",getEnumValue("客户小类",this.partEditCustomerFBBean.getSMALL_TYPE()));
            gr1.setFieldValue("DAYS",getEnumValue("信用天数",this.partEditCustomerFBBean.getDAYS()));
            gr1.setFieldValue("SETTLEMENT",this.partEditCustomerFBBean.getSETTLEMENT());
            gr1.setFieldValue("TAX_TYPE",getEnumValue("是否退税",this.partEditCustomerFBBean.getTAX_TYPE()));
            String Status = getEnumValue("客户状态",this.partEditCustomerFBBean.getStatus());
            log.info("Status = "+getEnumValue("客户状态",this.partEditCustomerFBBean.getStatus()));
            /*if(Status.equals("有效")){
                gr1.setFieldValue("STATUS","Active");
            }else if(Status.equals("无效")){
                gr1.setFieldValue("STATUS","Inactive");
            }*/
            gr1.setFieldValue("STATUS",Status);
            gr1.setFieldValue("REMARK","OA");

            gr1.setFieldValue("SALES_AREAS","");
            gr1.setFieldValue("PARENT_CUSTOMER","");
            gr1.setFieldValue("COMMISION_RATE","");
            gr1.setFieldValue("TS_DEPT_RATE","");
            gr1.setFieldValue("ACCEPT_BILL_DAYS","");
            gr1.setFieldValue("CUSTOMER_ASSIGNMENT","");
            gr1.setFieldValue("CUSTOMER_FUNCTION","");
            gr1.setFieldValue("TYPE","");
            gr1.setFieldValue("AREA","");
            gr1.setFieldValue("F_A","");
            gr1.setFieldValue("F_B","");
            gr1.setFieldValue("F_C","");
            gr1.setFieldValue("F_D","");
            gr1.setFieldValue("F_E","");
            grs1.add(gr1);
            GroupRecord gr2 = new GroupRecord();
            gr2.setName("DATAS");
            gr2.setSubGroups("value", grs1);
            grs2.add(gr2);
            reqMo.setReqGroupRecord(grs2);
            log.info("reqMo:"+reqMo);
            IMsgObject respMo = null;
            respMo = Requester.execute(reqMo);//发布报文

            log.info("respMo--result:"+respMo);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return true;
    }
    public String getEnumValue(String enumName,String value)throws Exception{
        log.info("value = "+value);
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
        conn.setAutoCommit(false);
        pst = conn.prepareStatement("select l.SHOWVALUE from CTP_ENUM t left join CTP_ENUM_ITEM l on t.ID = l.REF_ENUMID WHERE l.ENUMVALUE = ? AND t.ENUMNAME = ?");
        pst.setString(1,value);
        pst.setString(2,enumName);
        rs = pst.executeQuery();
        while (rs.next()) {
            log.info("getEnumValue = "+rs.getString(1));
            return rs.getString(1);
        }
        DBUtil.close(rs);
        return "";
    }
    public void setPartEditCustomerFBBean(PartEditCustomerFBBean partEditCustomerFBBean) {
        this.partEditCustomerFBBean = partEditCustomerFBBean;
    }
}
